package tech.bmatch.image;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.bmatch.image.model.Photo;
import tech.bmatch.image.model.User;
import tech.bmatch.image.service.Impl.UserStoreServiceImpl;
import tech.bmatch.image.service.UserService;

import java.io.*;
import java.util.regex.Pattern;


/**
 * @program:ApplyPhoto
 * @author:LinJinhao
 * @create:2018-10-29 20:01
 **/
public class Application{
    public static final Logger logger = LoggerFactory.getLogger(Application.class);
    public static UserService userService = new UserStoreServiceImpl();

    public static void main(String[] args) {

        logger.info("指令upload(上传)的输入格式:upload 用户id 图片路径 用户名");
        logger.info("指令browse(浏览)的输入格式:browse 用户名");
        logger.info("指令mark(水印)的输入格式:mark 图片名字 横坐标 纵坐标");
        logger.info("回车确认开始");
        BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(System.in));
        while(true){

            String command = null;

            try {
                command = bufferedReader.readLine();
            } catch (IOException e) {
                logger.error("请重新输入");
            }

            logger.info("执行命令:"+command);
            if (command.startsWith("upload ")){
                String content = command.replaceAll("upload ","");
                String[] items = content.split(" ");
                initUser(items);
                sourceData(items[0],items[1],items[2]);
            }
            if (command.startsWith("browse ")){
                String content = command.replaceAll("browse ","");
                initBrowse(content);
            }
            if (command.startsWith("mark ")){
                String content = (command.replaceAll("mark ", ""));
                String[] a = content.split(" ");
                String name = a[0];
                int x = Integer.valueOf(a[1]);
                int y = Integer.valueOf(a[2]);
                markImage(name,x,y);
            }
        }

    }

    /**
     * 准备数据传入
     * @Param: userId 用户id
     * @Param:  filePath 图片路径
     * @Param:  userName 用户名
     * @return:
    **/
    private static void  sourceData(String userId,String filePath,String userName){
        byte[] data =photoData(filePath);

        String name = filePath.substring(filePath.lastIndexOf("/")+1);
        System.out.println("用户名"+userName);

        userService.upload(userId,data,name,userName);
    }
    /*
    *上传图片
    * @Param: filePath：本地路径
    * */
    public static byte[] photoData(String filePath){
        byte[] data = null;
        try{

            File file = new File(filePath);
            FileInputStream in =new FileInputStream(file);
            data = new byte[in.available()];
            in.read(data);

            return data;
        }catch (Exception e){
            logger.error("读取本地路径发生错误!");
            e.printStackTrace();
        }
        return null;
    }

    /*
    * 浏览该用户的所有图片
    * @param
    * */
    public static void initBrowse(String userName){
        userService.browse(userName);
    }

    /*
    * 用户
    * @param
    * */
    public static void initUser(String[] items){
        User user = new User();
        user.setUserId(items[0]);
        user.setFilePath(items[1]);
        user.setUsername(items[2]);
        userService.uploadUser(user);
    }

    /**
     * @Param: name 照片名字
     * @Param:  x 打水印位置的横坐标
     * @Param:  y 打水印位置的纵坐标
     * @return:
    **/
    public static void markImage(String name,int x,int y){
        userService.markImageByIcon(name,x,y);
    }
}
